Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
let variable null when passing from parent to child | React

I have a variable that I don't want to bind to state in React. So I declared it as let with initial value as null. Later with event I set its value in parent and then pass it to child. But in child its value is getting null. Not sure what mistake I am making. Below is the code.

function Parent() {

    const[showChild, setShowChild] = useState(false);
    let data = null;

    const setData = () => {
      data = 'Test';
      setShowChild(true);
      console.log('function called');
    };

    return (
      <>
        <button onClick={setData}>
          Click Me
        </button>
        {showChild && <Child data={data} />}
      </>
    );
}

function Child({data}) {
    console.log('data '  + data);
    return (
      <>
        <h2>
          {data}
        </h2>
      </>
    );
}

ReactDOM.render(
  <Parent />, 
  document.getElementById('mountNode'),
);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you want to persist state you have to use useState as you did in the first line. so instead of manually let data ... and const setData you should have something like the following:

const [data, setData] = useState(null)

about 4 years ago · Juan Pablo Isaza Report

0

You sould use useState, whenever the state changes in parent, child will be render again.

I changed your example a little bit to show you what happen exactly, child just shows the value that parent has sent.

const {useState} = React;

const Parent=()=> {
 const[showChild, setShowChild] = useState(false);
 const[data, setData] = useState(0);
    //let data = null;

    const handleClick = () => {
      setData((prev=>prev+1));
      setShowChild(true);
      console.log('function called');
    };
    return (
      <div>
          <button onClick={handleClick}>
          Click Me
        </button>
        {showChild && <Child data={data} />}
      </div>
    );
}
const Child=({data})=> {
    console.log('data '  + data);
    return (
      <div>
        <h2>
          {data}
        </h2>
      </div>
    );
}


ReactDOM.render( <Parent/> ,
  document.getElementById("mountNode")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="mountNode"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!